package cn.edu.flink.tutorial.window;


import cn.edu.flink.tutorial.wordcount.flatmap.WordCountFlatMap;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class CountWindowWordCount {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//        DataStreamSource<String> text = env.readTextFile("src/main/resources/word.txt");
        DataStreamSource<String> text = env.socketTextStream("localhost", 8088);

        SingleOutputStreamOperator<Tuple2<String, Integer>> sum =
                text.flatMap(new WordCountFlatMap())
                .keyBy(0)
                .countWindow(10)
                .sum(1);

        sum.print().setParallelism(1);

        // execute program
        env.execute("WindowWordCount");
    }
}
